<template>
	<view class="width100" >
		<!--标题栏-->
		<bar-title :isSolid="false">
			<block slot="content">提现</block>
		</bar-title>
		
		<view class="cu-list menu card-menu margin-top">
			<view class="cu-item" >
				<view class="content text-df text-gray">
					<text class="text-black">真实姓名</text>
					<text class="margin-left" v-if="bankInfo && bankInfo.alipayaname">{{bankInfo.alipayaname}}</text>
				</view>
				<view class="action text-gray" @click="goPages('bankManage')">
					<text class="margin-right-xs" v-if="bankInfo.alipayaname">去更改</text>
					<text class="margin-right-xs" v-else>去添加</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view class="cu-item" >
				<view class="content text-df text-gray">
					<text class="text-black">到支付宝</text>
					<text class="margin-left">{{bankInfo.alipayaccount}}</text>
				</view>
			</view>
		</view>
		
		
		<view class="cu-list menu card-menu margin-top">
			<view class="cu-item" >
				<view class="content text-gray">
					<text >可提现：</text>
					<text class="text-price">{{balance}}</text>
				</view>
				<view class="action" @click="price = balance">
					<button class="cu-btn line-cyan sm round"
					>全部提现</button>
				</view>
			</view>
			<view class="cu-item" >
				<view class="content padding-bottom">
					<view class="margin-tb-sm">提现金额</view>
					<view class="flex baseline">
						<text class="">￥</text>
						<input type="digit" 
							class="input"
							placeholder="请输入提现金额"
							placeholder-style="color:#ddd;font-size:48rpx;"
							name="gameprice" 
							v-model="price"
							maxlength="8"
						></input>
						<text v-if="isExcess" class="text-cyan text-sm">超过可提现金额</text>
					</view>
				</view>
			</view>
		</view>
		
		<view class="padding text-sm text-gray">
			<view> 提现说明：</view>
			<view>1.提现成功后，将在T+1日内转入至绑定的银行卡/支付宝账户；</view>
			<view>2.每笔提现将收取1%的服务费。</view>
		</view>
		
		<view class="padding">
			<button class="block cu-btn bg-cyan lg"
				:disabled="isWidthdraw"
				@click="doWidthdraw"
			>确认提现</button>
		</view>
	</view>
</template>

<script>
import { User } from '@/models/user.js';
let user = new User()
console.log(user);
export default {
	data() {
		return {
			balance: 0,
			price: '',
			bankInfo: {},
		}
	},
	onLoad(option) {
		this.getFaultAccount()
	},
	onShow() {
		this.balance = user.balanceaccount
		this.bankInfo = user.faultAccount || {}
	},
	computed:{
		// ...mapState({
		// 	"bankInfo": state => state.bankInfo
		// }),
		isWidthdraw () {
			if (this.price!=0 && this.balance -0 >= this.price -0) {
				return false
			}
			return true
		},
		isExcess() {
			if (this.balance -0 < this.price -0) {
				return true
			}
			return false
		},
		bankSortNum () {
			let bankSortNum = '';
			if (this.bankInfo.bankcardnum) {
				var bankcardnum= this.bankInfo.bankcardnum
				bankSortNum = bankcardnum.substring(bankcardnum.length-4)
			}
			return bankSortNum
		}
	},
	methods:{
		goPages(pageName) {
			this.$jumpPages(pageName)
		},
		async getFaultAccount() {
			this.bankInfo = await user.getFaultAccount();
		},
		async doWidthdraw() {
			if (this.price -0 <= 0){
				uni.showToast({
					icon: 'none',
					title: '提现金额必须大于0元'
				});
				return
			}
			if (this.price -0 > this.balance -0){
				uni.showToast({
					icon: 'none',
					title: '账户余额不足'
				});
				return
			}
			let reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/;
			if (!reg.test(this.price)){
				uni.showToast({
					icon: 'none',
					title: '账户余额不足'
				});
				return
			}
			
			await this.$apis.withdrawal({
				bankcardid: this.bankInfo.bankcardid,
				withdrawalamount: this.price
			});
			this.$mTool.showModal({
				content: '提现申请已提交！',
				showCancel: false,
				success: res => {
					if (res.confirm) {
						this.$backPage()
					}
				}
			})
			
		},
		addBank() {
			uni.showModal({
				title: '您好：',
				content: '您还没有绑定银行卡？',
				cancelText: '取消',
				confirmText: '去绑定',
				success: res => {
					if (res.confirm) {
						this.jumpTypePages('modifyaccount', {}, 'replace')
					}else if (res.cancel) {
						uni.navigateBack(1)
					}
				}
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.cu-list{
	.baseline {
		align-items: baseline;
	}
	.input{
		flex: 1;
		width: 0;
		font-size: 52rpx;
	}
}
</style>
